package moriarty.gwt.client;

import moriarty.gwt.shared.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class JoinHandler implements ClickHandler, KeyUpHandler{

	private final SotdServiceAsync sotdService = GWT.create(SotdService.class);
	
	@Override
	public void onKeyUp(KeyUpEvent event) {
		onClick(null);
	}

	@Override
	public void onClick(ClickEvent event) {
		DialogBox newUserDialog = createDialogBox();
		newUserDialog.show();
	}

	private DialogBox createDialogBox() {
	    // Create a dialog box and set the caption text
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("SoTD");

		// Create a table to layout the content
		VerticalPanel dialogPanel = new VerticalPanel();
		dialogPanel.setSpacing(4);

		
		// Create a Grid for the text input and labels
		Grid newUserGrid = new Grid(6,2);

		// Error Label
		final Label errorLabel = new Label("Fill everything out dude");
		errorLabel.setStyleName("error-label");
		errorLabel.setVisible(false);
		
		// First Name
		Label firstName = new Label("First Name");
		final TextBox firstNameTextBox = new TextBox();
		
		// Last Name
		Label lastName = new Label("Last Name");
		final TextBox lastNameTextBox = new TextBox();
		
		// Nick Name
		Label alias = new Label("Display Alias");
		final TextBox aliasTextBox = new TextBox();
		
		// Email
		Label email = new Label("Email");
		final TextBox emailTextBox = new TextBox();
		emailTextBox.setText(CurrentUser.getInstance().getCurrentUserEmail());
		
		// Cancel button at the bottom of the dialog
		Button cancelButton = new Button("cancel", new ClickHandler() {
		      public void onClick(ClickEvent event) {
		        dialogBox.hide();
		      }
		    });

		// OK button, which will create the group
		Button okButton = new Button("ok", new ClickHandler() {
		      public void onClick(ClickEvent event) {	  
		    	
		    	String tmpfirstName = firstNameTextBox.getText();
		    	String tmplastName = lastNameTextBox.getText();
		    	String tmpalias = aliasTextBox.getText();
		    	String tmpemail = emailTextBox.getText();
		    	String tmpgmailemail = CurrentUser.getInstance().getCurrentUserEmail();
		    	
		    	if( tmpfirstName.isEmpty() || tmplastName.isEmpty() || tmpalias.isEmpty() || tmpemail.isEmpty()){
		    		errorLabel.setVisible(true);
		    	}
		    	else{
		    		errorLabel.setVisible(false);	    		
		        	User newUser = new User(tmpalias, tmpfirstName, tmplastName, tmpemail, tmpgmailemail);
		        	sotdService.storeUser(newUser, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) {
							errorLabel.setText("Unable to communicate with the SoTD Server. Try reloading the page...");
							errorLabel.setVisible(true);
							//dialogBox.hide();						
						}

						@Override
						public void onSuccess(Void result) {
							Window.Location.reload();					
						}
		        		
		        	});
		        	dialogBox.hide();
		    	}
		      }
		    });
		
		
		// Lay all the components into the grid
		newUserGrid.setWidget(0, 0, errorLabel);
		newUserGrid.setWidget(1, 0, firstName);
		newUserGrid.setWidget(1, 1, firstNameTextBox);
		newUserGrid.setWidget(2, 0, lastName); 
		newUserGrid.setWidget(2, 1, lastNameTextBox);
		newUserGrid.setWidget(3, 0, alias); 
		newUserGrid.setWidget(3, 1, aliasTextBox);
		newUserGrid.setWidget(4, 0, email); 
		newUserGrid.setWidget(4, 1, emailTextBox);
		newUserGrid.setWidget(5, 0, cancelButton);
		newUserGrid.setWidget(5, 1, okButton);
		VerticalPanel backPanel = new VerticalPanel();
		backPanel.add(newUserGrid);
		dialogBox.setWidget(backPanel);
		
		// Return the dialog box
		return dialogBox;
	}
	
}
